"""
@name         客服在途
@version      1.0
@time         2025/04/23 11:15:36
@description  Retrieve data from the customer service system to obtain the work order ID, business number, contact phone number, 
              reason feedback, and installation address, and then send them to the test group.

@author       liuYanXiang,652455342@qq.com
@match        http://10.238.1.245/
@Must-do      Fix DingDing roboot token and @-mentioned mobile number.
              The authorization is valid for 24 hours and may expire. Please obtain a new authorization before running. 
              If you have a better alternative to this, such as session, change it!
              6_Authorization.
"""
import requests
import urllib.parse
import time
import json
from datetime import datetime
from datetime import timedelta

title = "客服在途\n"
message = "客服在途\n"
def get_request_url():
    return "http://10.238.1.245/cs/pcc/aa/core/sheetquery/querylistbyes"

def get_authorization():
    try:
        with open('6_Authorization', 'r') as file:
            for line in file:
                if line:
                    return line
    except FileNotFoundError:
        print("9_Authorization file not found.")
    return None

def get_request_headers():
    authorization = get_authorization()
    return {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Authorization": authorization,
        "Cbss-Ms-Gray": "staffid=HEB3160248$departid=HB311e4eecd8ae29a4cdd94f197b7f25$provincecode=18$eparchycode=$citycode=$serialnumber=$userid=767a7e3139d34a92903bc2a9499cfaa5$custid=$acctid=$psptid=$ip=$origdomain=$osnduns=$routetype=$routevalue=$transido=$channelid=$channeltype=$accesstype=$processtime=",
        "Connection": "keep-alive",
        "Content-Length": "29837",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cs-Ms-Route": "18",
        "Host": "10.238.1.245",
        "Origin": "http://10.238.1.245",
        "Referer": "http://10.238.1.245/cs/pcc/aa/webcore/sheet/es/page/mixed-sheetquery-es-datagrid.html",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }


def get_request_data():
    now = datetime.now()
    start_date = (now - timedelta(hours=1)).replace(minute=0, second=0, microsecond=0)
    return {
        "match.tbl_gdzx_sheet_main__dlc_pro_id": "18",
        "like.tbl_gdzx_sheet_main__sheet_code": "",
        "and.tbl_gdzx_sheet_main__business_no": "",
        "timeBetween.tbl_gdzx_sheet_main__accept_time1": start_date.strftime('%Y-%m-%d %H:%M:%S'),
        "timeBetween.tbl_gdzx_sheet_main__accept_time2": now.strftime('%Y-%m-%d %H:%M:%S'),
        "in.tbl_gdzx_sheet_main__bus_region_id.keyword": "",
        "and.tbl_gdzx_sheet_main__indictseq": "",
        "and.tbl_gdzx_sheet_main__rc_sheet_code.keyword": "",
        "and.tbl_gdzx_sheet_main__is_over": "1",
        "and.tbl_gdzx_sheet_main__contact_no": "",
        "timeBetween.tbl_gdzx_sheet_main__deal_line1": "",
        "timeBetween.tbl_gdzx_sheet_main__deal_line2": "",
        "and.tbl_gdzx_sheet_main__accept_user": "",
        "and.tbl_gdzx_sheet_main__pd_user_code": "",
        "and.tbl_gdzx_sheet_main__this_user_code": "",
        "and.tbl_gdzx_sheet_main__this_user_name": "",
        "and.tbl_gdzx_sheet_answers__create_user.keyword": "",
        "and.tbl_gdzx_sheet_main__sheet_type_root": "",
        "orgName": "",
        "in.tbl_gdzx_sheet_main__code_sheet_type_id": "N2_GZ_01,N2_GZ_0101,N2_GZ_010101,N2_GZ_01010101,N2_GZ_01010102,N2_GZ_01010103,N2_GZ_01010104,N2_GZ_01010105,N2_GZ_01010106,N2_GZ_010102,N2_GZ_01010201,N2_GZ_01010202,N2_GZ_01010203,N2_GZ_010103,N2_GZ_01010301,N2_GZ_01010302,N2_GZ_01010303,N2_GZ_0102,N2_GZ_010201,N2_GZ_01020101,N2_GZ_01020102,N2_GZ_01020103,N2_GZ_01020104,N2_GZ_01020105,N2_GZ_01020106,N2_GZ_01020107,N2_GZ_01020108,N2_GZ_01020109,N2_GZ_010202,N2_GZ_01020201,N2_GZ_01020202,N2_GZ_01020203,N2_GZ_010203,N2_GZ_01020301,N2_GZ_01020302,N2_GZ_01020303,N2_GZ_0103,N2_GZ_0104,N2_GZ_010401,N2_GZ_010402,N2_GZ_010403,N2_GZ_010404,N2_GZ_010405,N2_GZ_010406,N2_GZ_010407,N2_GZ_0105,N2_GZ_010501,N2_GZ_0106,N2_GZ_010601,N2_GZ_010602,N2_GZ_010603,N2_GZ_010604,N2_GZ_010605,N2_GZ_010606,N2_GZ_010607,0650_N2,8dad40b9b415419eaaddda00c6d9c3ac,9bcc1051001b4d19b029034c00bd27d7,e451ecb6386348cfb548634670f26ee5,a21b0fb93d8848f1bbc035ad085bf2f7,31a50022b93b48878d3bdfcdff3c088a,7535502268624a3a9f3b94a083300_N2,6f54891ff6a74f9c87264776be612453,f508ae596e6240b288a69174c5db850b,f42b2fe8926e4d4fad8e2a8600108283,b381ed1128df446e9c2675e9e0d90a9d,db3b683bd11f4f7b88753e4ea27237f1,cc3f4e783217423d85455d753287b8ee,60468e2c6a294ebabe5ea25dfffb8474,f0fa0411cbf04918aa42531f5e32db4f,517a7dc28c394c8cb4b4443992fca111,02c70de53dd7466884dd4f1b9615fb11,606e74183fb84533b36f4109b89efe9d,a1cff3ee5a5345eeb06ac8bb27fb7e12,b1cc3340d706488eb95620f1657b07ed,1b2846971fd941cdad18778061f7ff0f,06207f1466a8487eb57b915ae476da86,46928d138ce749dc8fc409e8b1fe3d87,68c3dd37da414a6aad5334f027143091,abaf68a93a464a4b965eb497f346118f,d5506e2044e14860bea1f527475aa_N2,94f5e8f70cd74a899cbb0ffb8f80a_N2,368da7978ef6462091b20b3665211_N2,065001_N2,c8b4a5241cd24f7c821f04f7f5c5d_N2,f808b05548e64a49ab200cdf2a85a_N2,065001037_N2,840ff082b0b64f3b81f1903595c88_N2,065001039_N2,065001021_N2,065001020_N2,065001022_N2,065001023_N2,065001024_N2,065001025_N2,065001026_N2,065001027_N2,065001028_N2,065001029_N2,065001030_N2,065001031_N2,065001032_N2,065001033_N2,065001034_N2,065001035_N2,065001038_N2,065001036_N2,065001040_N2,065001040001_N2,065001040002_N2,065001040003_N2,065001040004_N2,065001040005_N2,065001040006_N2,065002_N2,d16f810c70ab40be911e9491077df_N2,3fe138ac3001432483e9b42bdf22a_N2,6023632ab4814c29b576a30066086_N2,065002006_N2,065002006001_N2,065002006002_N2,065002006003_N2,065002001_N2,065002002_N2,065002003_N2,065002004_N2,065002005_N2,065005_N2,065005001_N2,065005001001_N2,065005001002_N2,065005001003_N2,065005001004_N2,065005001005_N2,065005001006_N2,065005001007_N2,065005002_N2,065005002001_N2,065005003_N2,065005003001_N2,065005003002_N2,065005003003_N2,065005003004_N2,065005003005_N2,065005004_N2,065005004001_N2,065005005_N2,065005005001_N2,065008_N2,065008020_N2,065008021_N2,065008022_N2,065008023_N2,065008024_N2,065008025_N2,065008026_N2,065008027_N2,065008028_N2,065008029_N2,065008030_N2,065008031_N2,065008032_N2,065008033_N2,065008034_N2,065008035_N2,065008036_N2,065008037_N2,065008038_N2,065008039_N2,065008040_N2,065008040001_N2,065008040002_N2,065008040003_N2,065008040004_N2,065008040005_N2,065008040006_N2,6f1a354c82a04ae59db8f6838fb98_N2,bb040aa577f84d7da648bfa64f9bd_N2,065004_N2,2732695b80d247f09467f6247edc4_N2,065004030_N2,065004014_N2,065004011_N2,065004010_N2,065004016_N2,2a126eacbc0541118f99a8a72f093f20,065004012_N2,065004013_N2,065004015_N2,065004017_N2,065004018_N2,065004019_N2,065004020_N2,065004021_N2,065004022_N2,065004023_N2,065004024_N2,065004025_N2,065004026_N2,065004027_N2,065004028_N2,065004029_N2,065004031_N2,065004032_N2,3209053a83484e679ac78ceb29715dff,62f158226a884fd185784ce88621a11a,29aa77ffd48b48678acd55f3b230f678,05fde11c687d4bb9835bde673795eeaf,656643d76cb241fbbe7a672c0400a716,278454e876b345329bfea232fed585df",
        "in.tbl_gdzx_sheet_main__code_business_type": "",
        "in.tbl_gdzx_sheet_main__submit_way": "",
        "in.tbl_gdzx_sheet_main__code_accept_channel": "",
        "in.tbl_gdzx_sheet_main__sheet_status": "",
        "in.tbl_gdzx_sheet_main__org_id": "",
        "in.tbl_gdzx_sheet_main__customer_pro": "",
        "in.tbl_gdzx_sheet_main__complain_area": "0316",
        "text.tbl_gdzx_sheet_main__serv_content_new": "",
        "in.tbl_gdzx_sheet_main__is_direct_reply": "",
        "in.tbl_gdzx_sheet_main__code_urgent_level": "",
        "in.tbl_gdzx_sheet_main__code_important_type": "",
        "in.tbl_gdzx_sheet_main__dw_fault_location": "",
        "and.tbl_gdzx_sheet_main__dw_manager_range": "",
        "and.tbl_gdzx_sheet_main__caller_no": "",
        "isPromiseRel": "",
        "timeBetween.tbl_gdzx_sheet_main__end_time1": "",
        "timeBetween.tbl_gdzx_sheet_main__end_time2": "",
        "and.tbl_gdzx_sheet_main__principal_dept.keyword": "",
        "and.tbl_gdzx_sheet_main__first_principal.keyword": "",
        "and.tbl_gdzx_sheet_main__is_rc_sheet.keyword": "",
        "in.tbl_gdzx_sheet_answers__auto_code_satisfaction": "",
        "and.tbl_gdzx_sheet_answers__auto_is_solve": "",
        "tbl_gdzx_sheet_answers__auto_is_timely_contact": "",
        "in.tbl_gdzx_sheet_main__cd_yj_total_num": "",
        "in.tbl_gdzx_sheet_main__sheet_link_flag": "",
        "in.tbl_gdzx_sheet_main__dev_tendency": "",
        "in.tbl_gdzx_sheet_main__this_unit_id": "",
        "match.tbl_gdzx_sheet_main__dlc_deal_unit_id": "",
        "in.tbl_gdzx_sheet_ser__deal12_user_code.keyword": "",
        "in.tbl_gdzx_sheet_ser__deal12_unit_id.keyword": "18,HB3115b7d52b4a2abc58a2bc791ca2d7,HB3110719dda1e40a46ab839f0f08181,HB31174a7df312c0548e38e77787ae14,36ffc4c15e5e473b99da22ff77117bb4,26d6c200e13b4c609ea106b9f2020ae3,20082cd2996f4d4da750154249b37028,322dcec145164e98a0ba9a3eff8ac564,85d0726b26b14464a1b7c1a3cc1c6001",
        "in.tbl_gdzx_sheet_main__kh_customer_level": "",
        "in.tbl_gdzx_sheet_ser__first03_deal_unit_id.keyword": "",
        "timeBetween.tbl_gdzx_sheet_ser__dead_lineAC1": "",
        "timeBetween.tbl_gdzx_sheet_ser__dead_lineAC2": "",
        "and.tbl_gdzx_sheet_ser__charge_back_jd": "",
        "like.tbl_gdzx_sheet_ser__shensu_sheet_code_jd": "",
        "timeBetween.tbl_gdzx_sheet_ser__shensu_time1": "",
        "timeBetween.tbl_gdzx_sheet_ser__shensu_time2": "",
        "in.tbl_gdzx_sheet_ser__alik_event_num": "",
        "in.tbl_gdzx_sheet_ser__shensu_busi_code": "",
        "sheetTypeGroupName": "",
        "likeDm.tbl_gdzx_sheet_ser__unify_code": "",
        "in.tbl_gdzx_sheet_ser__expand_field_str13": "",
        "like.tbl_gdzx_sheet_ser__mark_jd": "",
        "in.tbl_gdzx_sheet_main__no_area": "",
        "and.tbl_gdzx_sheet_ser__visit_mark": "",
        "and.tbl_gdzx_sheet_main__proc_name": "",
        "in.tbl_gdzx_sheet_main__code_proc_type": "",
        "in.tbl_gdzx_sheet_main__expand_field8": "",
        "in.tbl_gdzx_sheet_main__is_repeat": "",
        "in.tbl_gdzx_sheet_main__up_Tendency": "",
        "in.tbl_gdzx_sheet_main__media_Tendency": "",
        "likes.tbl_gdzx_sheet_ser__expand_field_str69": "",
        "like.tbl_gdzx_sheet_ser__is_mix": "",
        "in.tbl_gdzx_customer_info__is_zq_svip": "",
        "text.tbl_gdzx_sheet_main__dlc_user_code": "",
        "isUpdateSheet": "",
        "timeBetween.tbl_gdzx_sheet_main__ex__link_arrive_time1": "",
        "timeBetween.tbl_gdzx_sheet_main__ex__link_arrive_time2": "",
        "in.tbl_gdzx_customer_info__is_carry_number": "",
        "and.tbl_gdzx_sheet_main__ex__professional_line": "",
        "numberBetween.tbl_gdzx_sheet_main__ex__professional_line_num": "",
        "and.tbl_gdzx_sheet_ser__restart_state": "",
        "and.tbl_gdzx_sheet_main__ex__suspend_state": "",
        "and.tbl_gdzx_sheet_main__ex__unsolvable_sheet": "",
        "match.tbl_gdzx_customer_info__caller_customer_type": "",
        "and.tbl_gdzx_sheet_main__total_line_time_out": "",
        "in.tbl_gdzx_customer_info__customer_prop": "",
        "in.tbl_gdzx_sheet_main__dw_complain_category": "",
        "in.tbl_gdzx_sheet_main__dw_duty_dept": "",
        "and.tbl_gdzx_sheet_ser__replace_complaint": "",
        "in.tbl_gdzx_sheet_main__ex__circulation_times.keyword": "",
        "and.tbl_gdzx_sheet_ser__main_or_assist": "",
        "text.tbl_gdzx_sheet_main__sheet_flag": "",
        "and.tbl_gdzx_sheet_main__is_appeal_user": "",
        "and.tbl_gdzx_sheet_main__ex__first_connect_time_value": "",
        "and.tbl_gdzx_sheet_main__ex__total_time_value": "",
        "and.tbl_gdzx_sheet_ser__is_tran_str": "",
        "and.tbl_gdzx_sheet_ser__is_first_contact_show": "",
        "and.tbl_gdzx_sheet_ser__first_contact_status": "",
        "and.tbl_gdzx_sheet_ser__is_explain": "",
        "and.tbl_gdzx_sheet_ser__is_suggest_visit": "",
        "and.tbl_gdzx_sheet_ser__is_because_region": "",
        "and.tbl_gdzx_sheet_ser__is_shensu10010": "",
        "and.tbl_gdzx_sheet_ser__is_shensu10015": "",
        "and.tbl_gdzx_sheet_main__upgrade_flag": "",
        "and.tbl_gdzx_sheet_main__promo_flag": "",
        "in.tbl_gdzx_customer_info__master_slave_flag": "",
        "and.tbl_gdzx_customer_info__is_complete_time_out": "",
        "and.tbl_gdzx_customer_info__up_tendency_code": "",
        "in.tbl_gdzx_sheet_main__kh_net_type": "",
        "and.tbl_gdzx_sheet_dealinfo__is_turn_province": "",
        "in.tbl_gdzx_sheet_main__ex__has_relation_sheet": "",
        "and.tbl_gdzx_sheet_main__language_type": "",
        "in.tbl_gdzx_sheet_main__from_sheet_field1": "",
        "and.tbl_gdzx_sheet_main__ex__is_effective_execution": "",
        "in.tbl_gdzx_fusing_record__fusing_type": "",
        "and.tbl_gdzx_sheet_main__kh_mail_address": "",
        "and.tbl_gdzx_sheet_main_ext__is_not_visit": "",
        "and.tbl_gdzx_sheet_main__ex__hottransfer_type": "",
        "numberBetween.tbl_gdzx_sheet_main__total_upgrade_num": "",
        "in.tbl_gdzx_sheet_main_ext__link_type03_number": "",
        "and.tbl_gdzx_sheet_main__is_delete": "0",
        "mixed": "true",
        "match.tbl_gdzx_sheet_main__ex__new_warn_pro_limit": "",
        "in.tbl_gdzx_sheet_main__ex__new_warn_promo_status_limit": "",
        "queryFields": "tbl_gdzx_promise_rel__sheet_id,tbl_gdzx_sheet_main__end_time,tbl_gdzx_sheet_main__expand_field8,tbl_gdzx_sheet_main__is_repeat,tbl_gdzx_sheet_main__up_Tendency,tbl_gdzx_sheet_main__media_Tendency,tbl_gdzx_sheet_main__deal_line,tbl_gdzx_sheet_main__code_important_type,tbl_gdzx_sheet_main__dw_fault_location,tbl_gdzx_sheet_main__code_urgent_level,tbl_gdzx_sheet_main__dlc_user_code,tbl_gdzx_sheet_main__sheet_id,tbl_gdzx_sheet_main__this_link_type,tbl_gdzx_sheet_main__sheet_code,tbl_gdzx_sheet_main__code_sheet_type_id,tbl_gdzx_sheet_main__business_no,tbl_gdzx_sheet_main__kh_customer_name,tbl_gdzx_sheet_main__org_id,tbl_gdzx_sheet_main__pro_id,tbl_gdzx_sheet_main__tenant_id,tbl_gdzx_sheet_main__sheet_status,tbl_gdzx_sheet_main__code_business_type,tbl_gdzx_sheet_main__sheet_type_root,tbl_gdzx_sheet_main__complain_area,tbl_gdzx_sheet_main__this_unit_id,tbl_gdzx_sheet_main__serv_content,tbl_gdzx_sheet_dealinfo__action_view,tbl_gdzx_sheet_dealinfo__deal_unit_id,tbl_gdzx_sheet_ser__deal12_user_code,tbl_gdzx_sheet_ser__deal12_unit_id,tbl_gdzx_sheet_main__kh_contact_name,tbl_gdzx_sheet_main__sheet_link_flag,tbl_gdzx_sheet_main__dw_problem_duty,tbl_gdzx_sheet_main__dw_duty_major,tbl_gdzx_sheet_main__code_accept_channel,tbl_gdzx_sheet_main__submit_way,tbl_gdzx_sheet_main__kh_customer_level,tbl_gdzx_sheet_dealinfo__user_code,tbl_gdzx_sheet_main__this_user_code,tbl_gdzx_sheet_main__this_user_name,tbl_gdzx_sheet_main__pd_user_code,tbl_gdzx_sheet_main__accept_time,tbl_gdzx_sheet_main__principal_dept,tbl_gdzx_sheet_main__first_principal,tbl_gdzx_sheet_main__is_over,tbl_gdzx_sheet_main__kh_mail_address,tbl_gdzx_sheet_main__expand_field9,tbl_gdzx_sheet_main__update_time,tbl_gdzx_sheet_main__total_line,tbl_gdzx_sheet_dealinfo__arrive_time,tbl_gdzx_sheet_ser__expand_field_str58,tbl_gdzx_sheet_ser__expand_field_str70,tbl_gdzx_sheet_ser__expand_field_str68,tbl_gdzx_sheet_main__ccc_contact_no,tbl_gdzx_sheet_main__catalog_path,tbl_gdzx_sheet_main__dev_tendency,tbl_gdzx_sheet_main__is_direct_reply,tbl_gdzx_sheet_main__ex__link_arrive_time,tbl_gdzx_sheet_answers__code_satisfaction,tbl_gdzx_sheet_answers__is_solve,tbl_gdzx_sheet_answers__code_answer_way,tbl_gdzx_sheet_answers__is_timely_contact,tbl_gdzx_sheet_main__indictseq,tbl_gdzx_sheet_main__rc_sheet_code,tbl_gdzx_sheet_main__dlc_deal_unit_id,tbl_gdzx_sheet_main__dlc_pro_id,tbl_gdzx_sheet_main__bus_region_id,tbl_gdzx_sheet_main__source_pro_id,tbl_gdzx_sheet_ser__unify_code,tbl_gdzx_sheet_main__cd_yj_total_num,tbl_gdzx_sheet_ser__expand_field_str21,tbl_gdzx_sheet_ser__is_from10015,tbl_gdzx_sheet_ser__sheet_id10015,tbl_gdzx_sheet_main__expand_field1,tbl_gdzx_sheet_ser__dead_line_jd,tbl_gdzx_sheet_ser__charge_back_jd,tbl_gdzx_sheet_ser__shensu_busi_code,tbl_gdzx_sheet_ser__alik_event_num,tbl_gdzx_sheet_ser__shensu_time,tbl_gdzx_sheet_ser__shensu_sheet_code_jd,tbl_gdzx_sheet_ser__is_mix,tbl_gdzx_sheet_ser__mark_jd,tbl_gdzx_customer_info__is_zq_svip,tbl_gdzx_customer_info__is_complete_time_out,tbl_gdzx_sheet_ser__is_upgrade_urge,tbl_gdzx_sheet_main__expand_field7,tbl_gdzx_sheet_main__ex__escalation_reminder,tbl_gdzx_sheet_ser__restart_state,tbl_gdzx_sheet_main__ex__suspend_state,tbl_gdzx_sheet_main__ex__unsolvable_sheet,tbl_gdzx_sheet_ser__replace_complaint,tbl_gdzx_sheet_ser__expand_field_str13,tbl_gdzx_sheet_ser__is_tran_str,tbl_gdzx_sheet_ser__is_first_contact_show,tbl_gdzx_sheet_ser__is_explain,tbl_gdzx_sheet_ser__is_suggest_visit,tbl_gdzx_sheet_ser__is_because_region,tbl_gdzx_sheet_ser__is_shensu10010,tbl_gdzx_sheet_ser__is_shensu10015,tbl_gdzx_customer_info__special_customer,tbl_gdzx_customer_info__customer_prop,tbl_gdzx_sheet_ser__expand_field_str69,tbl_gdzx_customer_info__is_carry_number,tbl_gdzx_sheet_main__ex__professional_line_num,tbl_gdzx_sheet_main__ex__professional_line,tbl_gdzx_sheet_main__auto_split_user,tbl_gdzx_sheet_main__auto_split_group_name,tbl_gdzx_sheet_main__ex_complain_product_code,tbl_gdzx_sheet_main__ex_complain_product_check_method,tbl_gdzx_sheet_main__ex_complain_product_name,tbl_gdzx_sheet_main__ex_complain_product_accept_date,tbl_gdzx_sheet_main__ex_complain_product_accept_channel,tbl_gdzx_sheet_main__ex_complain_product_accept_user,tbl_gdzx_sheet_main__ex_complain_product_start_date,tbl_gdzx_sheet_main__ex_complain_product_end_date,tbl_gdzx_sheet_main__is_rc_sheet.keyword,tbl_gdzx_customer_info__up_tendency_code,tbl_gdzx_sheet_main__ex__circulation_times,tbl_gdzx_sheet_main__ex__turn_reasons,tbl_gdzx_sheet_main__ex__firstvisit_usercode,tbl_gdzx_sheet_main__ex__firstvisit_username,tbl_gdzx_sheet_main__ex__firstvisit_department,tbl_gdzx_sheet_ser__main_or_assist,tbl_gdzx_sheet_main__sheet_flag,tbl_gdzx_sheet_answers__auto_code_satisfaction,tbl_gdzx_sheet_answers__auto_is_solve,tbl_gdzx_sheet_answers__auto_is_timely_contact,tbl_gdzx_sheet_answers__p_code_satisfaction,tbl_gdzx_sheet_answers__p_is_solve,tbl_gdzx_sheet_answers__p_is_timely,tbl_gdzx_sheet_ser__main_or_assist,tbl_gdzx_sheet_ser__visit_mark,tbl_gdzx_sheet_ser__first_contact_status,tbl_gdzx_sheet_determine__yunnan_sheet_flaga,tbl_gdzx_sheet_determine__yunnan_sheet_flagc,tbl_gdzx_sheet_determine__yunnan_sheet_flagd,tbl_gdzx_sheet_dealinfo__last_visit_time,tbl_gdzx_sheet_main__ex__total_time_value,tbl_gdzx_sheet_main__ex__pro_duct_type,tbl_gdzx_sheet_ser__duct_situation,tbl_gdzx_sheet_main__kh_net_type,tbl_gdzx_customer_info__caller_customer_type,tbl_gdzx_sheet_main__ex__first_connect_time_value,tbl_gdzx_sheet_main__customer_pro,tbl_gdzx_sheet_main__county_name,tbl_gdzx_sheet_main__unit_name,tbl_gdzx_sheet_main__ex__new_warn_msg,tbl_gdzx_sheet_main__ex__new_warn_pro,tbl_gdzx_sheet_main__ex__new_warn_promo_status,tbl_gdzx_sheet_main__ex__new_warn_promo_time,tbl_gdzx_sheet_main__ex__new_warn_msg_limit,tbl_gdzx_sheet_main__ex__new_warn_pro_limit,tbl_gdzx_sheet_main__ex__new_warn_promo_status_limit,tbl_gdzx_sheet_main__ex__new_warn_promo_time_limit,tbl_gdzx_sheet_dealinfo__is_turn_province,tbl_gdzx_sheet_dealinfo__turn_province_time,tbl_gdzx_sheet_dealinfo__turn_province_dept,tbl_gdzx_sheet_dealinfo__turn_province_user,tbl_gdzx_sheet_main__ex__has_relation_sheet,tbl_gdzx_sheet_main__language_type,tbl_gdzx_sheet_main__total_upgrade_num,tbl_gdzx_sheet_main__from_sheet_field1,tbl_gdzx_sheet_main__from_sheet_field2,tbl_gdzx_sheet_main__restart_times,tbl_gdzx_sheet_main_ext__is_not_visit,tbl_gdzx_sheet_main__ex__contact_length,tbl_gdzx_sheet_main__cbss_user_id,tbl_gdzx_sheet_main__ex__is_effective_execution,tbl_gdzx_sheet_main__proc_name,tbl_gdzx_sheet_main__accept_user,tbl_gdzx_sheet_main__ex__hottransfer_type,tbl_gdzx_sheet_main_ext__business_order_id",
        "tenantId": "UNICOM",
        "index": "pcc_sheet_mixed_comprehensive_current",
        "querySource": "gdcx",
        "page": "1",
        "rows": "500",
        "sort": "tbl_gdzx_sheet_main__accept_time",
        "direction": "desc"
    }


def send_request(url, headers, data):
    try:
        response = requests.post(url, headers=headers, data=urllib.parse.urlencode(data))
        response.raise_for_status()
        return response
    except requests.RequestException as e:
        print(f"请求发生错误: {e}")
        return None

def sendmessagetodingtalk(webhook_url, message, mobiles=[]):
    headers = {
        'Content-Type': 'application/json',
        'Charset': 'UTF-8'
    }

    payload = {
        'msgtype': 'text',
        'text': {
            'content': message
        },
        'at': {
            "atMobiles": mobiles,
            "isAtAll": False
        }
    }

    try:
        response = requests.post(webhook_url, headers=headers, data=json.dumps(payload))
        response.raise_for_status()
        result = response.json()
        if result.get('errcode') == 0:
            print('Message sent successfully.')
        else:
            print(f"Message sending failed, error message: {result.get('errmsg')}")
    except requests.RequestException as e:
        print(f'requests error: {e}')

def extract_info(response_text):
    try:
        data = json.loads(response_text)
        customer_info = data['RSP']['DATA']['main']
        address = data['RSP']['DATA']['sheetSer'].get('expandFieldStr9')
        business_no = customer_info.get('businessNo')
        contact_no = customer_info.get('callerNo')
        return business_no,contact_no,address
    except json.JSONDecodeError:
        print('JSON数据解析错误')
        return None

def extract_row(response_s_text):
    try:
        data = json.loads(response_s_text)
        info = data['RSP']['DATA']['rows'][1]
        appendhtml = info.get('appendRowHtml')
        return "".join(appendhtml.split("\t")).rstrip("<br>").lstrip("<br>")
    except json.JSONDecodeError:
        print('JSON数据解析错误')
        return None

def get_info(id,code):
    global message
    authorization = get_authorization()
    url = f"http://10.238.1.245/cs/pcc/business/sheetDetailJson/fullBuild?sheetId={id}"
    url_s = f"http://10.238.1.245/cs/pcc/business/sheet/dealinfo/getpagelistOperator?sheetId={id}&tenantId=UNICOM&page=1&rows=10&proId=18&sort=createTime&direction=desc&threeInterface=QK%2CCADS%2CGW%2CWY%2CQKJT%2CFWDT"
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Authorization': authorization,
        'Connection': 'keep-alive',
        'Cs-Ms-Route': '18',
        'Host': '10.238.1.245',
        'Referer': 'http://10.238.1.245/cs/pcc/react/app/',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
    }
    try:
        response = requests.get(url, headers=headers)
        time.sleep(0.3)
        response_s = requests.get(url_s,headers=headers)
        if response.status_code == 200 and response_s.status_code == 200:
           business_no,contact_no,address = extract_info(response_text=response.text)
           row_html = extract_row(response_s_text=response_s.text)
           message += f"{code} {business_no} {contact_no} {row_html} {address}\n"
           print(code,business_no,contact_no,row_html,address)
        else:
            print(f'请求失败，状态码: {response.status_code}')
    except requests.RequestException as e:
        print(f'请求发生错误: {e}')
    time.sleep(1)



def main():
    now = datetime.now()
    global message
    url = get_request_url()
    headers = get_request_headers()
    data = get_request_data()
    response = send_request(url, headers, data)
    if response:
        try:
            result = json.loads(response.text)
            for row in result["RSP"]["DATA"]["rows"]:
                tbl_gdzx_sheet_main__sheet_id = row.get("tbl_gdzx_sheet_main__sheet_id", "")
                tbl_gdzx_sheet_main__sheet_code = row.get("tbl_gdzx_sheet_main__sheet_code", "")
                get_info(tbl_gdzx_sheet_main__sheet_id,tbl_gdzx_sheet_main__sheet_code)
            message += f"\n查询时间: {(now - timedelta(hours=1)).replace(minute=0,second=0,microsecond=0)} ~ {now.strftime('%Y-%m-%d %H:%M:%S')}\n"
            print(message)
            sendmessagetodingtalk('https://oapi.dingtalk.com/robot/send?access_token=b48d67f748e84d15ccca047e932c6eb8de30f8f10c054909896d368af1e41368', message, [])
            message = "客服在途\n"
        except json.JSONDecodeError:
            print("无法解析响应为 JSON 格式。")


if __name__ == "__main__":
    while True:
        main()
        time.sleep(3600) #3600秒